package com.cskaoyan.service;

import com.cskaoyan.bean.MarketFeedback;
import com.cskaoyan.bean.MarketFeedbackExample;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.mapper.MarketFeedbackMapper;
import com.cskaoyan.util.MyBatisUtil;
import com.cskaoyan.util.StringUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.util.List;

public class AdminFeedbackServiceImpl implements AdminFeedbackService {
    @Override
    public CommonData<MarketFeedback> list(Integer id, String username, Integer page, Integer limit, String sort, String order) {


        MarketFeedbackExample marketFeedbackExample = new MarketFeedbackExample();
        MarketFeedbackExample.Criteria criteria = marketFeedbackExample.createCriteria();



        if (id != null) {

            criteria.andIdEqualTo(id);

        }
        if (!StringUtil.isEmpty(username)) {

            criteria.andUsernameLike("%" + username + "%");


        }

        criteria.andHasPictureEqualTo(true);

        if (!StringUtil.isEmpty(sort) && !StringUtil.isEmpty(order)) {
            marketFeedbackExample.setOrderByClause(sort + " " + order);
        }

        SqlSessionFactory sqlSessionFactory = MyBatisUtil.getSqlSessionFactory();

        SqlSession sqlSession = sqlSessionFactory.openSession();

        PageHelper.startPage(page, limit);

        MarketFeedbackMapper mapper = sqlSession.getMapper(MarketFeedbackMapper.class);

        List <MarketFeedback> marketFeedBacks = mapper.selectByExample(marketFeedbackExample);



        if (sqlSession != null) {

            sqlSession.close();

        }

        return   CommonData.data(new PageInfo(marketFeedBacks));





    }
}
